package com.sl.crawler.service.lianjia.xiaoqu;

import com.sl.crawler.domain.Xiaoqu;
import com.sl.crawler.manager.XiaoquManager;
import com.sl.crawler.robot.output.IOutput;
import com.sl.crawler.robot.CrawlerConfig;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by shiling on 2017/5/26.
 */
@Service
public class XiaoquMysqlOutputService implements IOutput {
    private final static Logger LOG = LoggerFactory.getLogger(XiaoquMysqlOutputService.class);

    @Autowired
    private XiaoquManager xiaoquManager;


    @Override
    public void output(List<Map<String, String>> list, CrawlerConfig crawlerConfig) {
        List<Xiaoqu> xiaoquList = buildXiaoquList(list);
        saveOrUpdateXiaoquList(xiaoquList);
    }

    private void saveOrUpdateXiaoquList(List<Xiaoqu> xiaoquList) {
        for (Xiaoqu xiaoqu : xiaoquList) {
            xiaoquManager.saveOrUpdateXiaoquAndDetail(xiaoqu);
        }
    }

    private static List<Xiaoqu> buildXiaoquList(List<Map<String, String>> resultList) {
        List<Xiaoqu> xiaoquList = new ArrayList<>();
        for (Map<String, String> map : resultList) {
            Xiaoqu xiaoqu = new Xiaoqu();
            try {
                BeanUtils.copyProperties(xiaoqu, map);
            } catch (Exception e) {
                e.printStackTrace();
            }
            buildXiaoquId(map, xiaoqu);
            buildHouseInfo(map, xiaoqu);
            buildPositionInfo(map, xiaoqu);

            xiaoquList.add(xiaoqu);
        }
        return xiaoquList;
    }

    /**
     * 获取位置信息
     *
     * @param map
     * @param xiaoqu
     */
    private static void buildXiaoquId(Map<String, String> map, Xiaoqu xiaoqu) {
        String regexPattern = "(\\d+)";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(map.get("xiaoquId"));
        if (matcher.find()) {
            xiaoqu.setXiaoquId(matcher.group());
        }
    }

    /**
     * 获取位置信息
     *
     * @param map
     * @param xiaoqu
     */
    private static void buildHouseInfo(Map<String, String> map, Xiaoqu xiaoqu) {
        String regexPattern = "共?(\\d*).*30天成交(\\d+)套\\s*\\|\\s*(\\d+)套正在出租";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(map.get("houseInfo"));
        if (matcher.find()) {
            String houseTypeCountStr = matcher.group(1);
            if (StringUtils.isNotBlank(houseTypeCountStr)) {
                xiaoqu.setHouseTypeCount(Integer.valueOf(houseTypeCountStr));
            }
            xiaoqu.setDealCount(Integer.valueOf(matcher.group(2)));
            xiaoqu.setLeaseCount(Integer.valueOf(matcher.group(3)));
        }
    }

    /**
     * 获取位置信息
     *
     * @param map
     * @param xiaoqu
     */
    private static void buildPositionInfo(Map<String, String> map, Xiaoqu xiaoqu) {
        String regexPattern = "/(.*)\\s*/\\s*(\\d+)";
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(map.get("positionInfo"));
        if (matcher.find()) {
            xiaoqu.setBuildingType(matcher.group(1));
            xiaoqu.setBuildingYears(Integer.valueOf(matcher.group(2)));
        }
    }
}
